package com.ming.springcloud.student.controller;

import com.ming.springcloud.student.pojo.Device;
import com.ming.springcloud.student.service.IDeviceService;
import com.ming.springcloud.user.api.util.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/device")
public class DeviceController {
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询所有书本信息
     *
     * @return
     */
    @RequestMapping("/findAllDevices")
    public List<Device> findAllStudent() {
        List<Device> DeviceList = new ArrayList<>();
        List<Device> devices = this.deviceService.findAll();
        for (Device device :devices ) {
            device.setServiceName("我是Service B");
            DeviceList.add(device);
        }
        return DeviceList;
    }

    /**
     * 根据id查询
     *
     * @param id
     * @param
     * @return
     */
    @RequestMapping("/findDevicesById/{id}")
    public Device findDataGroupById(@PathVariable("id")Integer id) {
        Device device = new Device();
        Optional<Device> devices = this.deviceService.findUserById(id);

        device=devices.get();
        device.setServiceName("我是Service B");

        return device;
    }

    /**
     * 更新服务器
     *
     * @param
     * @param
     * @return
     */
    //  @RequestMapping(value = "/editStudent" , method = )
    @PostMapping("/editDevice")
    public ResponseEntity editDataGroup(@RequestBody Device device) {
        device.setServiceName("我是Service B");
        this.deviceService.edit(device);
        return ResponseEntity.success();
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */

    @RequestMapping("/delDevice/{id}")
    public ResponseEntity delDataGroup(@PathVariable("id")Integer id) {
        this.deviceService.delete(id);
        return ResponseEntity.success();
    }

    /**
     * 添加学生
     * @param device
     * @param
     * @return
     */
    @RequestMapping("/addDevice")
    public ResponseEntity addStudent(@RequestBody Device device) {
        device.setServiceName("我是Service B");
        this.deviceService.save(device);
        return ResponseEntity.success();
    }
}
